﻿using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Data;
using RescueApp.Model;
using TodoAPP.UI.Common;
using RescueAPP.UI.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace RescueApp.UI
{
    public partial class AddRescueDispatch : Form
    {
        private RescueContext db;
        public AddRescueDispatch()
        {
            InitializeComponent();
        }
        public AddRescueDispatch(RescueContext db)
        {
            InitializeComponent();
            this.db = db;
        }
        private bool ValidateItem()
        {

            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("救援申请ID不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (!textBox2.Text.IsNum())
            {
                MessageBox.Show("救援申请ID必须是数字！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("救援车辆司机不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("救援车辆车牌号不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox5.Text))
            {
                MessageBox.Show("到达现场时间不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!DateTime.TryParse(textBox5.Text, out _))
            {
                MessageBox.Show("到达现场时间格式不正确，请输入有效的日期时间格式（例如：yyyy-MM-dd HH:mm）！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (string.IsNullOrEmpty(textBox6.Text))
            {
                MessageBox.Show("故障原因不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

           
            if (string.IsNullOrEmpty(textBox7.Text))
            {
                MessageBox.Show("救援费用不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (!float.TryParse(textBox7.Text, out _))
            {
                MessageBox.Show("救援费用格式不正确，请输入有效的数字！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!ValidateItem()) return;

            int id = int.Parse(textBox2.Text);

            //if (DbHelper.IsExistsWithORM(db, id))
            //{
            //    MessageBox.Show("该救援申请ID已存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

            RescueDispatch item = new RescueDispatch
            {
               
                RescueID = id,
                Driver = textBox3.Text.Trim(),
                LicenseID = textBox4.Text.Trim(),
                ArrivedTime = DateTime.Parse(textBox5.Text.Trim()),
                Cause = textBox6.Text.Trim(),
                Cost = float.Parse(textBox7.Text.Trim())
            };

            try
            {
                db.RescueDispatches.Add(item);
                db.SaveChanges();
                MessageBox.Show("填写成功!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                string errorMessage = $"Database error: {ex.Message}";
                if (ex.InnerException != null)
                {
                    errorMessage += $"\nInner Exception: {ex.InnerException.Message}";
                }
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void RescueDispatchForm_Load(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            // 清空之前的错误提示
            label2.ForeColor = Color.Black;
            label2.Text = "救援申请ID";

            if (string.IsNullOrEmpty(textBox2.Text))
            {
                return; // 如果输入为空，不进行验证
            }

            int id;
            if (!int.TryParse(textBox2.Text, out id))
            {
                MessageBox.Show("救援申请ID格式不正确，请输入有效的数字！", "输入失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.ForeColor = Color.Red;
                
                return;
            }

            // 检查 RescueApply 是否存在
            if (!DbHelper.IsExistsWithORM(db, id))
            {
                textBox2.ForeColor = Color.Red;
                MessageBox.Show("救援申请ID不存在，请输入一个有效的救援申请ID！", "输入失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
            }
            else
            {
                textBox2.ForeColor = Color.Green;
               
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
          
            

      
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
         

      
          
         }
    }
}
